<extend name="Layout/ins_page" />
<block name="content">
	<input type="hidden" name="ajax" id="ajax" value="1">
	<select name="folder_list" id="folder_list" class="hidden">
		<option value="0">根节点</option>
		{:fill_option($folder_list)}
	</select>
	{:W('PageHeader/simple',array('name'=>'后台任务'))}
	<div class="oprator-panel">
		<div class="split"></div>
		<a id="btn_add" class="btn">新增</a>
		<a id="btn_save" class="btn">保存</a>
		<span class="l">|</span>
		<a id="btn_del" class="btn btn-del">删除</a>
	</div>
	<div class="sub-frame">
		<div class="col-sm-6 sub-sidebar">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>名称</th>
						<th>命令</th>
						<th>规则</th>
						<th>状态</th>
					</tr>
				</thead>
				<tbody>
					<volist name="list" id="data">
						<tr id="{$data.id}">
							<td>{$data.name}</td>
							<td>{$data.command}</td>
							<td>每{$data.interval}
							<eq name="data.type" value="D">
								天 {$data.time|substr=0,5}执行一次
							</eq>
							<eq name="data.type" value="S">
								秒 执行一次
							</eq></td>
							<td>{$data.is_del|status}</td>
						</tr>
					</volist>
				</tbody>
			</table>
		</div>
		<div class="sub-content gray">
			<form id="form_data" name="form_data" method="post" >
				<input type="hidden" name="id" id="id" >
				<input type="hidden" name="opmode" id="opmode" value="">
				<input type="hidden" name="admin" id="admin">
				<input type="hidden" name="write" id="write">
				<input type="hidden" name="read" id="read">
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="name">名称*：</label>
					<div class="layui-input-block">
						<input class="layui-input" type="text" id="name" name="name" check="required" info="请输入名称">
					</div>
				</div>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="command">命令*：</label>
					<div class="layui-input-block">
						<input class="layui-input" type="text" id="command" name="command" check="required" info="请输入命令">
					</div>
				</div>
				<div class="layui-sub-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">类型*：</label>
						<label class="col-4">每</label>
						<div class="layui-input-inline col-5">
							<input type="text" class="layui-input"  name="interval" id="interval">
						</div>
						<div class="layui-input-inline col-9 x-select">
							<select class="layui-input" name="type" id="type" >
								<option>请选择</option>
								<option value="D">天</option>
								<option value="S">秒</option>
							</select>
						</div>
						<div class="layui-input-inline col-9" >
							<input class="layui-input hidden" type="time" id="time" name="time">
						</div>
					</div>
				</div>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="start_date">开始日期*：</label>
					<div class="layui-input-block">
						<input class="layui-input input-date" type="text" id="start_date" name="start_date" check="required" info="请选择开始日期">
					</div>
				</div>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="end_date">结束日期*：</label>
					<div class="layui-input-block">
						<input class="layui-input input-date" type="text" id="end_date" name="end_date" check="required" info="">
					</div>
				</div>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="sort">排序：</label>
					<div class="layui-input-block col-10">
						<input class="layui-input" type="text" id="sort" name="sort" >
					</div>
				</div>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="sort">状态：</label>
					<div class="layui-input-block col-10 x-select">
						<select class="layui-input"  name="is_del" id="is_del">
							<option  value="0">启用</option>
							<option value="1">禁用</option>
						</select>
					</div>
				</div>
				<div class="layui-sub-form-item">
					<label class="layui-form-label" for="remark" >备注：</label>
					<div class="layui-input-block" >
						<textarea class="layui-textarea" name="remark" rows="5"></textarea>
					</div>
				</div>
			</form>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		layui.use('global', function() {
			var $ = layui.jquery;
			set_return_url(null);

			$(".sub-sidebar tbody tr").click(function() {
				$(".sub-sidebar  tr.active").removeClass("active");
				$(this).attr("class", "active");
				send_ajax("{:U('read_time')}", "id=" + $(this).attr("id"), function(data) {
					showdata(data);
				});
				return false;
			});

			function showdata(result) {
				for (var s in result.data) {
					set_val(s, result.data[s]);
					if (s == 'type') {
						if (result.data[s] == 'D') {
							$("#time").removeClass('hidden');
						} else {
							$("#time").addClass('hidden');
						}
					}
				}
				$("#opmode").val("edit");
			};

			$("#btn_save").on('click', function() {
				send_form("form_data", "{:U('save_timer')}", "{:U('timer')}", function(ret) {
					if (ret.status) {
						layer.msg(ret.info, {
							time : 1200
						}, function() {
							location.reload(true);
						});
					} else {
						layer.msg(ret.info);
						return false;
					}
				});
			});

			$("#btn_add").on('click', function() {
				winopen("{:U('add_timer')}", 560, 470);
			});

			$('#btn_del').on('click', function() {
				if ($(".sub-sidebar tr.active").length == 0) {
					layer.msg('请选择要删除的任务');
					return false;
				}
				$('#opmode').val('del');
				//信息框-例2
				layer.msg('确定要删除吗?', {

					time : 0,
					btn : ['确定', '取消'],
					yes : function(index) {
						var vars = $("#form_data").serialize();
						send_ajax("{:U('save_timer')}", vars, function(ret) {
							if (ret.status) {
								layer.msg(ret.info, function() {
									location.reload(true);
								});
							}
						});
					},
					no : function(index) {
						layer.close(index);
					}
				});
			});

			$("#type").on('change', function() {
				if ($("#type").val() == 'D') {
					$("#time").removeClass('hidden');
				} else {
					$("#time").addClass('hidden');
				}
			});
		});

	</script>
</block>